package com.company.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.common.persistence.Page;
import com.base.common.service.CrudService;
import com.base.common.utils.SQLUtil;
import com.base.common.utils.StringUtils;
import com.base.db.DBManageService;
import com.company.dao.CompanyProjectMapper;
import com.company.model.CompanyProject;
import com.company.service.ProjectManageService;
import com.company.service.ProjectWbsService;

@Service
public class ProjectManageServiceImpl extends
		CrudService<CompanyProjectMapper, CompanyProject> implements
		ProjectManageService {

	@Autowired
	ProjectWbsService projectWbsService;

	public List<CompanyProject> findProjectListByParams(String companyId,
			String projectName, String projectNo, String endFlag)
			throws Exception {

		Map<String, String> params = new HashMap<String, String>();
		params.put("companyId", companyId);
		String sql = "SELECT t.id,t.project_name,t.project_no,t.start_date,t.plan_end_date,t.end_date,case t.flag when'1' then '自主'  "
				+ "when'2' then '委托' when'3' then '合作' else '集中' end as flag,case t.expensing_or_capital when'1' then '费用化' else '资本化' end as expensing_or_capital "
				+ ",t.budget,t.content,t.company_id from company_project t where t.company_Id = #{companyId} ";

		if (StringUtils.isNotEmpty(projectName)) {
			sql += " and t.project_name like '%" + projectName + "%' ";
		}
		if (StringUtils.isNotEmpty(projectNo)) {
			sql += " and t.project_no = '" + projectNo + "' ";
		}
		if (StringUtils.isNotEmpty(endFlag) && endFlag.equals("1")) {
			sql += " and t.end_date is not null ";
		} else if (StringUtils.isNotEmpty(endFlag) && endFlag.equals("2")) {
			sql += " and t.end_date is null ";
		}
		List<CompanyProject> list = this.getList(sql, CompanyProject.class, params);
		return list;
	}

	public int findProjectCountByCompanyId(String companyId, Page page)
			throws Exception {

		Map<String, String> params = new HashMap<String, String>();
		params.put("companyId", companyId);
		String sql = "SELECT * from company_project t where t.company_Id = #{companyId} ";
		List<CompanyProject> list = this.getList(sql, CompanyProject.class,
				params);
		return list.size();
	}

	public List<CompanyProject> findProjectByCompanyId(String companyId)
			throws Exception {
		Map<String, String> params = new HashMap<String, String>();
		params.put("companyId", companyId);
		String sql = "SELECT * from company_project t where t.company_Id = #{companyId} ";
		List<CompanyProject> list = this.getList(sql, CompanyProject.class,
				params);
		return list;
	}

	public List<CompanyProject> findEndProjectByCompanyId(String companyId,
			String endFlag) throws Exception {
		Map<String, String> params = new HashMap<String, String>();
		params.put("companyId", companyId);
		String sql = "SELECT * from company_project t where t.company_Id = #{companyId}";
		if (endFlag.equals("1")) {
			sql += " and t.end_date is NOT null";
		} else {
			sql += " and t.end_date is null";
		}
		List<CompanyProject> list = this.getList(sql, CompanyProject.class,
				params);
		return list;
	}

	public boolean deleteProjectByIds(String ids) throws Exception {
		String sql = "delete from company_project where id in ("
				+ SQLUtil.createInStr(ids) + ") ";
		int res = this.delete(sql);
		return true;
	}

	public boolean saveProject(CompanyProject companyProject) throws Exception {
		int res = this.saveOrUpdate(companyProject);
		projectWbsService.saveProjectWbs(companyProject.getId());
		return true;
	}

}
